package com.notetool.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

/*
 * 
 * 
 * Expected behavior:
 * 
 * "."        -> StrippedName : ""
 *            ->Extension     : ""
 *            
 * ".hey"     -> StrippedName : ""
 *            -> Extension    : "hey"
 *            
 * "hey."     -> StrippedName : "hey"
 *            -> Extension    : ""
 *            
 * "hey"      -> StrippedName : "hey"
 *            -> Extension    : null
 *            
 * ""         -> StrippedName : ""
 *            -> Extension    : null
 *            
 * "hey.test" -> StrippedName : "hey"
 *            -> Extension    : "test"
 * 
 */

public class FileHelper
{

    ////////////////////
    // Public static methods
    public static String getName (File file)
    {
        // Not dealing with null input
        if (file == null)
            return null;
        
        // Not doing anything that isn't a file
        if (!file.isFile ())
            return null;
        
        // Extract name
        String name = file.getName ();
        
        // Make sure it isn't null
        if (name == null)
            return null;
        
        return name;
    }
    
    public static String getExtension (File file)
    {
        // Get file name
        String name = getName (file);
        
        // Get extension
        return getExtension (name);
    }
    
    public static String getExtension (String name)
    {
        // Not dealing with no imput
        if (name == null || name.isEmpty ())
            return null;
        
        // Reverse the filename
        String reversedName = new StringBuffer (name).reverse ().toString ();
        
        // Find the first "."
        int index = reversedName.indexOf ('.');
        
        if (index == -1)
            return null;
        
        // Extract extension
        return name.substring (name.length () - index);
    }
    
    public static String getStrippedName (File file)
    {
        // Get file extension
        String name = getName (file);
        
        // Get stripped name
        return getStrippedName (name);
    }
    
    public static String getStrippedName (String name)
    {
        if (name == null)
            return null;
        
        // Get extension
        String extension = getExtension (name);
        
        // If no extension, return full name
        if (extension == null)
            return name;
        
        // If extension, grab first part
        return name.substring (0, name.length () - extension.length () - 1);
    }
    
    public static String getLine (File file, int index)
    {
        if (file == null)
            return null;
        
        if (index < 0)
            return null;
        
        // Declare reader here so that the finally block can close it...
        BufferedReader reader = null;
        
        try
        {
            // Open reader
            reader = new BufferedReader (new FileReader (file));
            
            // Read up to the line before the one we want
            for (int i = 0; i < index; i ++)
            {
                if (reader.readLine () == null)
                    return null;
            }
            
            // Read the right line
            return reader.readLine ();
        }
        catch (Exception ex)
        {
            return null;
        }
        finally
        {
            // Close reader
            try {reader.close ();} catch (IOException ioe) {}
        }
    }
    
}
